/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import wads.com.UserInfo;
import wads.com.UserInfoDataStruct;
import wads.com.WADTools;
import wads.database.Derby;

public class UserManagerFrame extends JDialog {
  
  private static final String PU_FLAG = "PU";
  private static final String RU_FLAG = "RU";

  //private String fileName;
  //private File powerUserFile;
  //private File restrictUserFile;
  //***No use file to store Users data anymore.***
  
  private JTabbedPane UMFTabbedPane = new JTabbedPane(); //Have 2 Tab for PU Tab and RU Tab
  
  //Power User's Controls
  private JPanel jpPUPanel = new JPanel(); //Panel for PU Tab
  private JPanel jpPUInnerPanel = new JPanel(); //Panel for stock all PU Control
  private TitledBorder PUTitledBorder;
  private JLabel PUUsernameLabel = new JLabel();
  private JComboBox PUComboBox = new JComboBox();
  private JLabel PUNameLabel = new JLabel();
  private JTextField PUNameTF = new JTextField();
  private JLabel PUPasswordLabel = new JLabel();
  private JButton PUPasswordButton = new JButton();
  private JLabel PUPositionLabel = new JLabel();
  private JTextField PUPositionTF = new JTextField();
  private JLabel PUEmailLabel = new JLabel();
  private JTextField PUEmailTF = new JTextField();
  private JButton newPUButton = new JButton();
  private JButton delPUButton = new JButton();
  private JButton updatePUButton = new JButton();
  private JButton resetPUButton = new JButton();

  //Restricted User's Controls
  private JPanel jpRUPanel = new JPanel(); //Panel for RU Tab
  private JPanel jpRUInnerPanel = new JPanel();  //Panel for stock all RU Control
  private JLabel RUUsernameLabel = new JLabel();
  private JComboBox RUComboBox = new JComboBox();
  private JLabel RUNameLabel = new JLabel();
  private JTextField RUNameTF = new JTextField();
  private JLabel RUPasswordLabel = new JLabel();
  private JButton RUPasswordButton = new JButton();
  private JLabel RUPositionLabel = new JLabel();
  private JTextField RUPositionTF = new JTextField();
  private JLabel RUEmailLabel = new JLabel();
  private JTextField RUEmailTF = new JTextField();
  private JButton newRUButton = new JButton();
  private JButton delRUButton = new JButton();
  private JButton updateRUButton = new JButton();
  private JButton resetRUButton = new JButton();

  //Public Controls
  //private JButton saveButton = new JButton(); Remove this button because it replaced by 'Close' button instead.
  //private JButton cancelButton = new JButton();
  private JButton closeButton = new JButton();

  private UserInfo PUUserInfo;
  private UserInfo RUUserInfo;

//  private MySql db;
  private Derby db;

  public UserManagerFrame(WADDiagramEditor owner, String file) {
    super(owner, "User Manage Configure");
    this.setSize(530, 380);
    this.setModal(true);

    db = owner.getDb();
    //Load Power User profile
    PUUserInfo = owner.getPUUserInfo();
    RUUserInfo = owner.getRUUserInfo();

    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setResizable(false);
    PUTitledBorder = new TitledBorder("");
    this.getContentPane().setLayout(null);
    jpPUPanel.setLayout(null);
    
    PUComboBox.setBounds(new Rectangle(142, 28, 162, 21));
    PUComboBox.addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        PUComboBox_itemStateChanged(e);
      }
    });

    PUUsernameLabel.setText("Username:");
    PUUsernameLabel.setBounds(new Rectangle(50, 29, 80, 17));
    PUUsernameLabel.setForeground(Color.black);

    jpPUInnerPanel.setBorder(PUTitledBorder);
    jpPUInnerPanel.setBounds(new Rectangle(27, 56, 400, 187));
    jpPUInnerPanel.setLayout(null);

    PUNameLabel.setText("Name");
    PUNameLabel.setBounds(new Rectangle(10, 25, 35, 17));
    PUNameLabel.setForeground(Color.black);

    PUNameTF.setBounds(new Rectangle(95, 23, 280, 21));

    newPUButton.setText("New");
    newPUButton.setBounds(new Rectangle(435, 60, 75, 27));
    newPUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        newPUButton_actionPerformed(e);
      }
    });

    delPUButton.setText("Delete");
    delPUButton.setBounds(new Rectangle(435, 102, 75, 27));
    delPUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        delPUButton_actionPerformed(e);
      }
    });

    updatePUButton.setText("Update");
    updatePUButton.setBounds(new Rectangle(435, 144, 75, 27));
    updatePUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        updatePUButton_actionPerformed(e);
      }
    });

    resetPUButton.setText("Reset");
    resetPUButton.setBounds(new Rectangle(435, 186, 75, 27));
    resetPUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        resetPUButton_actionPerformed(e);
      }
    });
    resetPUButton.setEnabled(false);

    UMFTabbedPane.setBounds(new Rectangle(1, 1, 520, 294));

//    saveButton.setText("Save");
//    saveButton.setBounds(new Rectangle(350, 310, 80, 25));
//    saveButton.addActionListener(new ActionListener() {
//      @Override
//      public void actionPerformed(ActionEvent e) {
//        saveButton_actionPerformed(e);
//      }
//    });

    closeButton.setText("Close");
    closeButton.setBounds(new Rectangle(440, 310, 80, 25));
    closeButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        closeButton_actionPerformed(e);
      }
    });

    PUPasswordLabel.setBounds(new Rectangle(10, 65, 75, 17));
    PUPasswordLabel.setText("Password");
    
    PUPasswordButton.setText("Change...");
    PUPasswordButton.setBounds(new Rectangle(95, 63, 90, 21));
    PUPasswordButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
        PUPasswordButton_actionPerformed(evt);
      }
    });

    PUPositionLabel.setText("Position");
    PUPositionLabel.setBounds(new Rectangle(11, 103, 63, 19));
    PUPositionLabel.setForeground(Color.black);

    PUPositionTF.setBounds(new Rectangle(95, 101, 280, 21));
    PUPositionTF.setText("");

    PUEmailLabel.setForeground(Color.black);
    PUEmailLabel.setBounds(new Rectangle(12, 144, 43, 19));
    PUEmailLabel.setText("E-mail");

    PUEmailTF.setBounds(new Rectangle(95, 142, 280, 21));

    jpRUPanel.setLayout(null);
    RUComboBox.addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        RUComboBox_itemStateChanged(e);
      }
    });
    RUComboBox.setBounds(new Rectangle(142, 28, 162, 21));

    RUUsernameLabel.setForeground(Color.black);
    RUUsernameLabel.setBounds(new Rectangle(50, 29, 80, 17));
    RUUsernameLabel.setText("Username:");
    
    RUPasswordButton.setText("Change...");
    RUPasswordButton.setBounds(new Rectangle(95, 63, 90, 21));
    RUPasswordButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
        RUPasswordButton_actionPerformed(evt);
      }
    });

    RUNameTF.setBounds(new Rectangle(95, 23, 280, 21));

    RUPositionTF.setBounds(new Rectangle(95, 101, 280, 21));
    RUPositionTF.setVisible(false);

    RUEmailLabel.setText("E-mail");
    RUEmailLabel.setBounds(new Rectangle(12, 144, 43, 19));
    RUEmailLabel.setForeground(Color.black);

    RUPositionLabel.setForeground(Color.black);
    RUPositionLabel.setBounds(new Rectangle(11, 103, 43, 19));
    RUPositionLabel.setText("Position");
    RUPositionLabel.setVisible(false);

    RUEmailTF.setBounds(new Rectangle(95, 142, 280, 21));

    RUPasswordLabel.setText("Password");
    RUPasswordLabel.setBounds(new Rectangle(10, 65, 75, 17));
    RUPasswordLabel.setForeground(Color.black);

    RUNameLabel.setForeground(Color.black);
    RUNameLabel.setBounds(new Rectangle(10, 25, 35, 17));
    RUNameLabel.setText("Name");

    jpRUInnerPanel.setLayout(null);
    jpRUInnerPanel.setBounds(new Rectangle(27, 56, 400, 187));
    jpRUInnerPanel.setBorder(PUTitledBorder);

    newRUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        newRUButton_actionPerformed(e);
      }
    });
    newRUButton.setBounds(new Rectangle(435, 60, 75, 27));
    newRUButton.setText("New");
    
    delRUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        delRUButton_actionPerformed(e);
      }
    });
    delRUButton.setBounds(new Rectangle(435, 102, 75, 27));
    delRUButton.setText("Delete");

    updateRUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        updateRUButton_actionPerformed(e);
      }
    });
    updateRUButton.setBounds(new Rectangle(435, 144, 75, 27));
    updateRUButton.setText("Update");

    resetRUButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        resetRUButton_actionPerformed(e);
      }
    });
    resetRUButton.setBounds(new Rectangle(435, 186, 75, 27));
    resetRUButton.setText("Reset");
    resetRUButton.setEnabled(false);

    this.getContentPane().add(UMFTabbedPane, null);
    UMFTabbedPane.addTab("Powered User", jpPUPanel);
    jpPUPanel.add(PUComboBox, null);
    jpPUPanel.add(PUUsernameLabel, null);
    jpPUPanel.add(jpPUInnerPanel, null);
    jpPUInnerPanel.add(PUNameLabel, null);
    jpPUInnerPanel.add(PUNameTF, null);
    jpPUInnerPanel.add(PUPositionTF, null);
    jpPUInnerPanel.add(PUPasswordButton, null);
    jpPUInnerPanel.add(PUPasswordLabel, null);
    jpPUInnerPanel.add(PUEmailLabel, null);
    jpPUInnerPanel.add(PUPositionLabel, null);
    jpPUInnerPanel.add(PUEmailTF, null);
    jpPUPanel.add(updatePUButton, null);
    jpPUPanel.add(newPUButton, null);
    jpPUPanel.add(delPUButton, null);
    jpPUPanel.add(resetPUButton, null);

    UMFTabbedPane.addTab("Restricted User", jpRUPanel);
    jpRUPanel.add(RUComboBox, null);
    jpRUPanel.add(RUUsernameLabel, null);
    jpRUPanel.add(jpRUInnerPanel, null);
    jpRUInnerPanel.add(RUNameLabel, null);
    jpRUInnerPanel.add(RUNameTF, null);
    jpRUInnerPanel.add(RUPositionTF, null);
    jpRUInnerPanel.add(RUPasswordButton, null);
    jpRUInnerPanel.add(RUPasswordLabel, null);
    jpRUInnerPanel.add(RUEmailLabel, null);
    jpRUInnerPanel.add(RUPositionLabel, null);
    jpRUInnerPanel.add(RUEmailTF, null);
    jpRUPanel.add(updateRUButton, null);
    jpRUPanel.add(newRUButton, null);
    jpRUPanel.add(delRUButton, null);
    jpRUPanel.add(resetRUButton, null);
//    this.getContentPane().add(saveButton, null);
    this.getContentPane().add(closeButton, null);

    listDataPowerUser();
    initFieldPowerUser();

    listDataRestrictUser();
    initFieldRestrictUser();

    if((PUComboBox.getSelectedItem().toString()).equals("new")){
        PUNameTF.setText("");
        RUNameTF.setText("");
        PUPositionTF.setText("");
        RUPositionTF.setText("");
        PUEmailTF.setText("");
        RUEmailTF.setText("");
     }
  }

  //Method for list Username of Power User to ComboBox
  public void listDataPowerUser(){
    if(!PUUserInfo.isEmpty()){
      PUComboBox.removeAllItems();
      for(int i=0; i<PUUserInfo.getSize(); i++)
        PUComboBox.addItem(PUUserInfo.getUserDataAt(i).getUsername());
    }
  }

  //Method for list Username of Restricted User to ComboBox
  public void listDataRestrictUser(){
    if(!RUUserInfo.isEmpty()){
      RUComboBox.removeAllItems();
      for(int i=0; i<RUUserInfo.getSize();i++)
        RUComboBox.addItem(RUUserInfo.getUserDataAt(i).getUsername());
    }
  }

  //Method for fill information of Selected Power User from ComboBox
  //in each TextField for each data of that USER
  public void initFieldPowerUser(){
    if(!PUUserInfo.isEmpty()){
      if(PUComboBox.getSelectedIndex() != -1) {
        int index = PUComboBox.getSelectedIndex();
        PUNameTF.setText(PUUserInfo.getUserDataAt(index).getName());
        PUPositionTF.setText(PUUserInfo.getUserDataAt(index).getPosition());
        PUEmailTF.setText(PUUserInfo.getUserDataAt(index).getEMail());
      }
      else {
        PUNameTF.setText("");
        PUPositionTF.setText("");
        PUEmailTF.setText("");
      }
    }
  }

  //Method for fill information of Selected Restricted User from ComboBox
  //in each TextField for each data of that USER
  public void initFieldRestrictUser(){
    if(!RUUserInfo.isEmpty()){
      if(RUComboBox.getSelectedIndex() != -1){
        int index = RUComboBox.getSelectedIndex();
        RUNameTF.setText(RUUserInfo.getUserDataAt(index).getName());
        RUPositionTF.setText(RUUserInfo.getUserDataAt(index).getPosition());
        RUEmailTF.setText(RUUserInfo.getUserDataAt(index).getEMail());
      }
      else {
        RUNameTF.setText("");
        RUPositionTF.setText("");
        RUEmailTF.setText("");
      }
    }
  }

//  public void deleteListPowerUser(){
//    if(!PUUserInfo.isEmpty()){
//      if(PUComboBox.getSelectedIndex() != -1){
//        int index = PUComboBox.getSelectedIndex();
//        PUUserInfo.deleteData(index);
//        listDataPowerUser();
//        initFieldPowerUser();
//        WADTools.deleteUser(index);
//      }
//    }
//    else {
//        PUComboBox.removeAllItems();
//        PUNameTF.setText("");
//        PUPositionTF.setText("");
//        PUEmailTF.setText("");
//    }
//  }

//  public void deleteListRestrictUser(){
//    if(!RUUserInfo.isEmpty()){
//      if(RUComboBox.getSelectedIndex() != -1){
//        int index = RUComboBox.getSelectedIndex();
//        RUUserInfo.deleteData(index);
//        listDataRestrictUser();
//        initFieldRestrictUser();
//      }
//    }
//    else{
//      RUComboBox.removeAllItems();
//      RUNameTF.setText("");
//      RUPositionTF.setText("");
//      RUEmailTF.setText("");
//    }
//  }

  private void PUComboBox_itemStateChanged(ItemEvent e) {
    initFieldPowerUser();
  }
  
  private void PUPasswordButton_actionPerformed(ActionEvent e){
    showPasswordDialog("PU");
  }

  private void newPUButton_actionPerformed(ActionEvent e) {
    String username = JOptionPane.showInputDialog(this, "Enter Powered Username [Case Sensitive precedenced]");
    if(username == null)//Cancel Button Pressed
      return;
    else if(isEmptyStringInput(username))//OK Button Pressed but Input is Empty
      JOptionPane.showMessageDialog(this, "Input is Empty cannot Proceed", "Input is Empty String", JOptionPane.ERROR_MESSAGE);
    else if(isDuplicated(username, "PU") || db.isDuplicated(username))//OK Button Pressed but Input is Duplicated
      JOptionPane.showMessageDialog(this, "Input is Duplicated", "Duplicated Input", JOptionPane.ERROR_MESSAGE);
    else{
      UserInfoDataStruct data = new UserInfoDataStruct(username);
      data.setRoles("pu");
      PUUserInfo.addData(data);
      listDataPowerUser();

      db.insertUser(data);
      data.setId(db.loadUser(data.getUsername()).getId());

      PUComboBox.setSelectedIndex(PUComboBox.getItemCount()-1);
      PUNameTF.setText("");
      PUPositionTF.setText("");
      PUEmailTF.setText("");
    }
  }

  private void delPUButton_actionPerformed(ActionEvent e) {
    int delConfirmation = JOptionPane.showConfirmDialog(this ,
        "Are you sure that want to Delete this User \""+PUComboBox.getSelectedItem().toString()+"\"",
        "Delete User Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
    
    if(delConfirmation == JOptionPane.YES_OPTION) {
//      deleteListPowerUser();
        if(!PUUserInfo.isEmpty()){
            if(PUComboBox.getSelectedIndex() != -1){
                int index = PUUserInfo.getUserDataAt(PUComboBox.getSelectedIndex()).getId();
                PUUserInfo.deleteData(PUComboBox.getSelectedIndex());
                listDataPowerUser();
                initFieldPowerUser();
                db.deleteUser(index);
            }
        }
        else {
            PUComboBox.removeAllItems();
            PUNameTF.setText("");
            PUPositionTF.setText("");
            PUEmailTF.setText("");
        }
    }
    else
        return;
  }

  private void resetPUButton_actionPerformed(ActionEvent e) {
    initFieldPowerUser();
  }

  private void updatePUButton_actionPerformed(ActionEvent e) {
    updateUserInfomation(PU_FLAG, e);
  }

  private void RUComboBox_itemStateChanged(ItemEvent e) {
    initFieldRestrictUser();
  }
  
  private void RUPasswordButton_actionPerformed(ActionEvent e){
    showPasswordDialog("RU");
  }

  private void newRUButton_actionPerformed(ActionEvent e) {
    String username = JOptionPane.showInputDialog(this,"Enter Restricted Username [Case Sensitive precedenced]");
    if(username == null)//Cancel Button Pressed
      return;
    else if(isEmptyStringInput(username))//OK Button Pressed but Input is Empty
      JOptionPane.showMessageDialog(this, "Input is Empty cannot Proceed", "Input is Empty String", JOptionPane.ERROR_MESSAGE);
    else if(isDuplicated(username, "RU") || db.isDuplicated(username))//OK Button Pressed but Input is Duplicated
      JOptionPane.showMessageDialog(this, "Input is Duplicated", "Duplicated Input", JOptionPane.ERROR_MESSAGE);
    else{
      UserInfoDataStruct data = new UserInfoDataStruct(username);
      data.setRoles("ru");
      RUUserInfo.addData(data);
      listDataRestrictUser();

      db.insertUser(data);
      data.setId(db.loadUser(data.getUsername()).getId());

      RUComboBox.setSelectedIndex(RUComboBox.getItemCount()-1);
      RUNameTF.setText("");
      RUPositionTF.setText("");
      RUEmailTF.setText("");
    }
  }

  private void updateRUButton_actionPerformed(ActionEvent e) {
    updateUserInfomation(RU_FLAG, e);
  }

  private void delRUButton_actionPerformed(ActionEvent e) {
    int delConfirmation = JOptionPane.showConfirmDialog(this,
        "Are you sure that want to delete this User \""+RUComboBox.getSelectedItem().toString()+"\"",
        "Delete User Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
    
    if(delConfirmation == JOptionPane.YES_OPTION) {
        if(!RUUserInfo.isEmpty()) {
            if(RUComboBox.getSelectedIndex() != -1) {
                int index = RUUserInfo.getUserDataAt(RUComboBox.getSelectedIndex()).getId();
                RUUserInfo.deleteData(RUComboBox.getSelectedIndex());
                listDataRestrictUser();
                initFieldRestrictUser();
                db.deleteUser(index);
            }
        }
        else {
            RUComboBox.removeAllItems();
            RUNameTF.setText("");
            RUPositionTF.setText("");
            RUEmailTF.setText("");
        }
    }
    else
        return;
  }

  private void resetRUButton_actionPerformed(ActionEvent e) {
    initFieldRestrictUser();
  }
  
//  private void saveButton_actionPerformed(ActionEvent e) {
//    updateUserInfomation(PU_FLAG, e);
//    updateUserInfomation(RU_FLAG, e);
//    saveUserFile();
//
//    this.dispose();
//  }

  private void closeButton_actionPerformed(ActionEvent e) {
    this.dispose();
  }
  
  private boolean isEmptyStringInput(String u){
    if(u.equals(""))
      return true;
    else
      return false;
  }
  
  private boolean isDuplicated(String u, String userType){
    JComboBox ucb;
    if(userType.equals("PU"))
      ucb = PUComboBox;
    else if(userType.equals("RU"))
      ucb = RUComboBox;
    else
      ucb = new JComboBox();
    
    for(int i=0;i<ucb.getItemCount();i++)
      if(u.equals(ucb.getItemAt(i).toString()))
        return true;
    
    return false;
  }
  
  private void showPasswordDialog(String user_flag){
    int user_index;
    if(user_flag.equals(PU_FLAG))
      user_index = PUComboBox.getSelectedIndex();
    else if(user_flag.equals(RU_FLAG))
      user_index = RUComboBox.getSelectedIndex();
    else
      return;
    
    Object[] message = new Object[8];
    message[0] = new JLabel("Please fill the Password you want following these field.");
    message[1] = new JLabel("");
    message[2] = new JLabel("Enter Old Password (If New User; Password = 999)");
    JPasswordField opw = new JPasswordField();
    message[3] = opw;
    message[4] = new JLabel("Enter New Password.");
    JPasswordField npw = new JPasswordField();
    message[5] = npw;
    message[6] = new JLabel("Enter New Password again.");
    JPasswordField npwa = new JPasswordField();
    message[7] = npwa;
    
    String[] option = {"OK", "Cancel"};
    
    int result = JOptionPane.showOptionDialog(this, message, "Password Dialog", JOptionPane.DEFAULT_OPTION,
        JOptionPane.INFORMATION_MESSAGE, null, option, option[1]);
    
    switch(result){
      case 0: //yes
        managePassword(opw.getPassword(), npw.getPassword(), npwa.getPassword(), user_index, user_flag);
        break;
      case 1: //cancel
        System.out.println("No Way");
        break;
      default:
        break;
    }
  }
  
  private void managePassword(char[] opw, char[] npw, char[] npwa, int user_index, String user_flag){
    UserInfo ui;
    
    if(user_flag.equals(PU_FLAG))
      ui = PUUserInfo;
    else if(user_flag.equals(RU_FLAG))
      ui = RUUserInfo;
    else
      ui = null;
    
    if(!ui.getUserDataAt(user_index).isPasswordMatch(WADTools.encryptPassword(opw)))
      JOptionPane.showMessageDialog(this, "Mismatch with Old Password.", "Wrong password!", JOptionPane.ERROR_MESSAGE);
    else if(npw.length < 1 || npwa.length < 1)
      JOptionPane.showMessageDialog(this, "Cannot use blank password", "Blank password not permitted", JOptionPane.ERROR_MESSAGE);
    else if(!WADTools.isPasswordMatch(npw, npwa))
      JOptionPane.showMessageDialog(this, "2 new password inputs is not equal.", "Unequal 2 new password!", JOptionPane.ERROR_MESSAGE);
    else{
      ui.getUserDataAt(user_index).setEncryptedPassword(WADTools.encryptPassword(npw));
      db.updateUser(ui.getUserDataAt(user_index));
      JOptionPane.showMessageDialog(this, "Change Password Complete", "Succeed", JOptionPane.INFORMATION_MESSAGE);
    }
  }

//  *** No use of this method anymore. ***
//  private void saveUserFile(){
//    try{
//      PUOutputStream = new ObjectOutputStream(new FileOutputStream(powerUserFile));
//      RUOutputStream = new ObjectOutputStream(new FileOutputStream(WADTools.RESTRICT_USER_FILE));
//      PUOutputStream.writeObject(PUUserInfo);
//      RUOutputStream.writeObject(RUUserInfo);
//      PUOutputStream.close();
//      RUOutputStream.close();
//    }
//    catch(Exception ex) {
//      System.out.println("Exception thrown from UserManagerFrame.saveUserFile() .");
//      ex.printStackTrace();
//    }
//  }
  
  private void updateUserInfomation(String userflag, ActionEvent ae) {
    if(userflag.equals(PU_FLAG)) {
      if(PUNameTF.getText().equals(""))
        PUNameTF.setText(" ");
      if(PUPositionTF.getText().equals(""))
        PUPositionTF.setText(" ");
      if(PUEmailTF.getText().equals(""))
        PUEmailTF.setText(" ");

      //Get user that try to change attribute value
      UserInfoDataStruct data = PUUserInfo.getUserDataAt(PUComboBox.getSelectedIndex());
      //Update value
      data.setName(PUNameTF.getText());
      data.setPosition(PUPositionTF.getText());
      data.setEMail(PUEmailTF.getText());
    
      PUUserInfo.updateData(data, PUComboBox.getSelectedIndex());

      db.updateUser(data);
    }
    else if(userflag.equals(RU_FLAG)) {
      if(RUNameTF.getText().equals(""))
        RUNameTF.setText(" ");
      if(RUPositionTF.getText().equals(""))
        RUPositionTF.setText(" ");
      if(RUEmailTF.getText().equals(""))
        RUEmailTF.setText(" ");

      //Get user that try to change attribute value
      UserInfoDataStruct data = RUUserInfo.getUserDataAt(RUComboBox.getSelectedIndex());
      //Update value
      data.setName(RUNameTF.getText());
      data.setPosition(RUPositionTF.getText());
      data.setEMail(RUEmailTF.getText());
      
      RUUserInfo.updateData(data, RUComboBox.getSelectedIndex());

      db.updateUser(data);
    }
    else {
      System.out.println("Unknown flag.");
    }
  }
}
